package com.gxa.service.Impl;

import com.gxa.common.ResponseData;
import com.gxa.mapper.DeptMapper;
import com.gxa.pojo.Dept;
import com.gxa.pojo.Emp;
import com.gxa.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService{
    @Autowired(required = false)
    private DeptMapper deptMapper;
    //查询部门信息
    @Override
    public ResponseData queryDeptList(Integer currentPage, Integer pageSize)   {
        ResponseData rd = new ResponseData();
        Integer totalSize = deptMapper.countDept();
        rd.setCount(totalSize);
        Integer params1 = (currentPage - 1)*pageSize;
        Integer params2 = pageSize;
        List<Dept> deptList = deptMapper.queryDeptList(params1,params2);
        for(Dept dept:deptList){
            int dept_level = dept.getDept_level();
            if(dept_level == 1){
                dept.setDept_super("一级部门");
            }else{
                int dept_id = dept.getDept_fid();
                String dept_super = deptMapper.queryDeptSuper(dept_id);
                dept.setDept_super(dept_super);
            }
        }
        rd.setCode(200);
        rd.setMsg("操作成功");
        rd.setData(deptList);
        return rd;
    }
    // 添加部门
    @Override
    public ResponseData addDept(Dept dept) {
        ResponseData rd = new ResponseData();
        String dept_name = dept.getDept_name();
        Dept deptByname = deptMapper.queryDeptName(dept_name);
        if(deptByname == null){
            //        String emp_name = dept.getEmp_number();
            int dept_fid = dept.getDept_fid();
//        List<Emp> empList = deptMapper.queryEmpName(emp_name);
//        if(empList != null && !empList.isEmpty()){
            if(dept_fid == 0){
                dept.setDept_level(1);
                int result1 = deptMapper.addDept(dept);
                if(result1 > 0){
                    rd.setCode(200);
                    rd.setMsg("添加部门成功");
                }
            }else{
                int dept_id = dept_fid;
                int deptLevel = deptMapper.queryDeptLevel(dept_id);
                dept.setDept_level(deptLevel+1);
                int result1 = deptMapper.addDept(dept);
                if(result1 > 0){
                    rd.setCode(200);
                    rd.setMsg("添加部门成功");
                }
            }
//        }else {
//            rd.setCode(500);
//            rd.setMsg("该员工不存在！");
//        }
        }else {
            rd.setCode(500);
            rd.setMsg("该部门已存在！");
        }
        return rd;
    }
    // 修改部门信息
    @Override
    public ResponseData updateDept(Dept dept) {
        ResponseData rd = new ResponseData();
        int result = deptMapper.updateDept(dept);
        if(result > 0){
            rd.setCode(200);
            rd.setMsg("编辑部门信息成功");
        }
        return rd;
    }
    // 回显编辑部门信息
    @Override
    public ResponseData editDept(Integer dept_id) {
        ResponseData rd = new ResponseData();
        Dept dept = deptMapper.editDept(dept_id);
        if(dept != null){
            rd.setCode(200);
            rd.setMsg("操作成功");
            rd.setData(dept);
        }
        return rd;
    }
     // 删除部门信息
    @Override
    public ResponseData delDept(Integer dept_id,Integer dept_level) {
        ResponseData rd = new ResponseData();
        if(dept_level == 1 || dept_level== 2){
            List<Dept> deptList = deptMapper.queryDeptFid(dept_id);
            if(deptList == null || deptList.isEmpty()){
                int result1 = deptMapper.delDept(dept_id);
                if(result1 > 0) {
                    rd.setCode(200);
                    rd.setMsg("删除部门成功");
                }
            }else {
                rd.setCode(500);
                rd.setMsg("该部门下有下级部门，请删除下级部门在删除该部门");
            }
        }else{
            int result = deptMapper.delDept(dept_id);
            if(result > 0){
                rd.setCode(200);
                rd.setMsg("删除部门成功");
            }
        }
        return rd;
    }
    // 关键字查找部门信息
    @Override
    public ResponseData searchDeptList(Integer currentPage, Integer pageSize, String keyword) {
        ResponseData rd = new ResponseData();
        Integer totalSize = deptMapper.countSearchDeptList(keyword);
        Integer params1 = (currentPage - 1)*pageSize;
        Integer params2 = pageSize;
        List<Dept> deptList = deptMapper.searchDeptList(params1,params2,keyword);
        for(Dept dept:deptList){
            int dept_level = dept.getDept_level();
            if(dept_level == 1){
                dept.setDept_super("一级部门");
            }else{
                int dept_id = dept.getDept_fid();
                String dept_super = deptMapper.queryDeptSuper(dept_id);
                dept.setDept_super(dept_super);
            }
        }
        rd.setCode(200);
        rd.setMsg("操作成功");
        rd.setData(deptList);
        rd.setCount(totalSize);
        return rd;
    }
    //点击查询员工信息
    @Override
    public ResponseData queryEmpList(String dept_name) {
        ResponseData rd = new ResponseData();
        List<Emp> empList = deptMapper.queryEmpList(dept_name);
        rd.setCode(200);
        rd.setMsg("操作成功");
        rd.setData(empList);
        return rd;
    }
    // 查询部门组织架构
    @Override
    public ResponseData queryDept() {
        ResponseData rd = new ResponseData();
        List<Dept> deptList = deptMapper.queryDept();
        rd.setCode(200);
        rd.setMsg("操作成功");
        rd.setData(deptList);
        return rd;
    }
    // 查询一级部门
    @Override
    public ResponseData queryDept1List() {
        ResponseData rd = new ResponseData();
        List<Dept> deptList = deptMapper.queryDept1List();
        rd.setCode(200);
        rd.setMsg("操作成功");
        rd.setData(deptList);
        return rd;
    }
    // 查询二级部门
    @Override
    public ResponseData queryDept2List() {
        ResponseData rd = new ResponseData();
        List<Dept> deptList = deptMapper.queryDept2List();
        rd.setCode(200);
        rd.setMsg("操作成功");
        rd.setData(deptList);
        return rd;
    }
    // 查询三级部门
    @Override
    public ResponseData queryDept3List() {
        ResponseData rd = new ResponseData();
        List<Dept> deptList = deptMapper.queryDept3List();
        rd.setCode(200);
        rd.setMsg("操作成功");
        rd.setData(deptList);
        return rd;
    }

}
